package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.utils.IdGenerator;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.dao.ReqModelDetailMapper;
import com.chinairi.mall.dao.ReqModelMapper;
import com.chinairi.mall.model.ReqModel;
import com.chinairi.mall.model.ReqModelDetail;
import com.chinairi.mall.service.ICodeGeneratorService;
import com.chinairi.mall.service.IReqModelDetailService;
import com.chinairi.mall.service.IReqModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 需求模型业务逻辑层实现类
 * </p>
 * @author yyp
 * @since 2020-08-19
 */
@Service
public class ReqModelServiceImpl extends ServiceImpl<ReqModelMapper, ReqModel> implements IReqModelService {

    @Autowired
    private ICodeGeneratorService codeGeneratorService;

    @Autowired
    private ReqModelMapper reqModelMapper;

    @Autowired
    private IReqModelDetailService reqModelDetailService;

    @Autowired
    private ReqModelDetailMapper reqModelDetailMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> saveReqModel(ReqModel reqModel) {
        try {
            reqModel.setModelNo(codeGeneratorService.getReqModelNo());
            reqModel.setModelId(IdGenerator.UUID());
            reqModel.setCreateTime(new Date());
            reqModel.setCreateUser(UserSession.getUserId());
            reqModel.setStatus("1");
            reqModelMapper.insert(reqModel);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        return ResponseUtil.toSuccessMessage("保存成功", reqModel);
    }

    @Override
    public List<Map<String, Object>> pageReqModel(ReqModel reqModel) {
        return reqModelMapper.pageReqModel(reqModel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> updateReqModel(ReqModel reqModel) {
        try {
            reqModel.setUpdateTime(new Date());
            reqModel.setUpdateUser(UserSession.getUserId());
            reqModelMapper.updateById(reqModel);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        return ResponseUtil.toSuccessMessage("保存成功", reqModel);
    }

    @Override
    public Map<String, Object> getReqModel(String modelId) {
        Map<String, Object> reqModel = reqModelMapper.getReqModel(modelId);
        Map<String, Object> columnMap = new HashMap<>(1);
        columnMap.put("MODEL_ID", reqModel.get("modelId"));
        List<ReqModelDetail> reqModelDetailList = reqModelDetailMapper.selectByMap(columnMap);
        reqModel.put("reqModelDetailList", reqModelDetailList);
        return reqModel;
    }

    @Override
    public Map<String,Object> removeReqModel(String modelId){
        if (StringUtils.isEmpty(modelId)){
            return ResponseUtil.toWarningMessage("数据为空",false);
        }
       ReqModel reqModel= reqModelMapper.selectById(modelId);
       String status = reqModel.getStatus();
       reqModel.setStatus("1".equals(status)?"0":"1");
        try {
            this.updateById(reqModel);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败", false);
        }
        return ResponseUtil.toSuccessMessage("修改成功",reqModel);
    }


    private void saveReqModelDetail(ReqModelDetail reqModelDetail) {
        try {
            reqModelDetail.setCreateTime(new Date());
            reqModelDetail.setCreateUser(UserSession.getUserId());
            reqModelDetail.setUpdateTime(new Date());
            reqModelDetail.setUpdateUser(UserSession.getUserId());
            reqModelDetail.setModelDetailId(IdGenerator.UUID());
            reqModelDetailService.save(reqModelDetail);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Float modelAverageValue(String modelId) {
        if (StringUtils.isEmpty(modelId)) {
            return null;
        }
        Float modelAverageValue = reqModelMapper.culculatorAverageValue(modelId)==null?0:reqModelMapper.culculatorAverageValue(modelId);
        return modelAverageValue;
    }

    @Override
    public Map<String,Object> importReqModelDetail(ReqModel reqModel, String modelId){
        try {
            Map<String,Object> map= new HashMap<String,Object>(1);
            map.put("MODEL_ID",modelId);
            reqModelDetailService.removeByMap(map);
           List<ReqModelDetail> reqModelDetailList =  reqModel.getReqModelDetailList();
            if (reqModelDetailList.size() > 0) {
                reqModelDetailList.forEach(reqModelDetail -> {
                    reqModelDetail.setModelId(modelId);
                    this.saveReqModelDetail(reqModelDetail);
                });
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("保存失败",false);
        }
        return ResponseUtil.toSuccessMessage("保存成功",reqModel);
    }

    @Override
    public Map<String, Object> deleteReqModel(String modelId) {
        try{
            List<String> list=reqModelDetailMapper.getReqModelDetailIdListByModelId(modelId);
            if(list!=null&&list.size()>0){
                reqModelDetailMapper.deleteBatchIds(list);
            }
            reqModelMapper.deleteById(modelId);
        }catch(Exception e){
            e.printStackTrace();
            return ResponseUtil.toWarningMessage("删除失败",false);
        }
        return ResponseUtil.toSuccessMessage("删除成功",modelId);
    }

    @Override
    public List<Map<String, Object>> listYesOrNo() {
        return reqModelMapper.listYesOrNo();
    }

    @Override
    public Map<String, Object> checkModelName(String modelName) {
        QueryWrapper<ReqModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MODEL_NAME", modelName);
        if (reqModelMapper.selectList(queryWrapper) != null && reqModelMapper.selectList(queryWrapper).size() > 0) {
            return ResponseUtil.toWarningMessage("模型名称不能重复",modelName);
        }
        return ResponseUtil.toSuccessMessage("",modelName);
    }
}
